package com.becca.cake.service.address.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.becca.cake.core.exception.ServiceException;
import com.becca.cake.dao.address.AddressDAO;
import com.becca.cake.model.address.CityModel;
import com.becca.cake.model.address.ProvinceModel;
import com.becca.cake.model.address.RegionModel;
import com.becca.cake.model.users.UserAddressModel;
import com.becca.cake.service.address.IAddressService;

/**
 * 地址管理service实现类
 * @copyright  尊星(上海)信息科技有限公司
 * @author 
 * @date 2016年5月23日 下午12:10:51
 */
@Service
public class AddressServiceImpl implements IAddressService {
    @Autowired
    private AddressDAO addressDAO;

    @Override
    public List<ProvinceModel> queryProvinces(Map<String, Object> map) throws Exception {
        return addressDAO.queryProvinces(map);
    }

    @Override
    public List<CityModel> queryCitysByProvinceId(String provinceId) throws Exception {
        return this.addressDAO.queryCitysByProvinceId(provinceId);
    }

    @Override
    public List<RegionModel> queryRegionsByCityId(String cityId) throws Exception {
        return this.addressDAO.queryRegionsByCityId(cityId);
    }

    @Override
    public List<UserAddressModel> selectAddressByUserId(Map<String, Object> map)
                                                                                throws ServiceException {
        return this.addressDAO.selectAddressByUserId(map);
    }

    @Override
    public Long selectUserAddressCount(Map<String, Object> map) throws ServiceException {
        return this.addressDAO.selectUserAddressCount(map);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void updateAdressStatus(Map<String, Object> map) throws ServiceException {
        Long isForbidden = this.addressDAO.selectOneAddress(map);
        // 判断该地址是否已被禁用
        if (isForbidden != 0) {
            throw new ServiceException(UserAddressModel.ADDRESS_IS_FORBIDDEN);
        }
        this.addressDAO.updateAdressStatus(map);
    }

    /**
     * 设置默认地址
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void updateAdressDefault(Map<String, Object> map) throws ServiceException {
        Long isDefault = this.addressDAO.isAddressDefault(map);
        if (isDefault != 0) {
            throw new ServiceException(UserAddressModel.ADDRESS_IS_DEFAULT);
        }
        // 设为默认地址
        this.addressDAO.updateAdressDefault(map);
        map.put("isDefault", 0);
        // 其他地址取消默认
        this.addressDAO.updateAdressDefault(map);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void addOrUpdUserAddress(UserAddressModel userAddressModel) throws ServiceException {
        //step1:数据校验
        UserAddressModel.validateAddOrUpdUserAddress(userAddressModel);
        if (userAddressModel.getId() == null) {
            // 新添地址
            userAddressModel.setIsDefault(UserAddressModel.ADDRESS_NO_DEFAULT);
            this.addressDAO.insertUserAddress(userAddressModel);
        } else {
            // 修改地址
            this.addressDAO.updateUserAddress(userAddressModel);
        }
    }

    @Override
    public UserAddressModel selectUserAddressById(Integer id) throws ServiceException {
        return this.addressDAO.selectUserAddressById(id);
    }
    
}
